// pages/reservation/index.ts
import {UserDto} from "../../api/user"
import {ReservationDto,save} from "../../api/reservation"
import { LENGYU_SUCCESS_CODE } from "../../utils/request"


Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:{} as UserDto,
    form:{} as ReservationDto,
    loading:false,
    rules: [
      {
        name: 'applyName',
        rules: {required: true, message: '请填写访客姓名'},
      },
      {
        name: 'applyPhone',
        rules: {required: true, message: '请填写访客手机号'},
      },
      {
        name: 'applyCompany',
        rules: {required: true, message: '请填写访客公司'},
      },
      {
        name: 'visitName',
        rules: {required: true, message: '请填写被访人姓名'},
      },
      {
        name: 'visitPhone',
        rules: {required: true, message: '请填写被访人手机号'},
      },
      {
        name: 'startDateStr',
        rules: {required: true, message: '请填写到访日期'},
      },
      {
        name: 'startTimeStr',
        rules: {required: true, message: '请填写到访时间'},
      },
      {
        name: 'endDateStr',
        rules: {required: true, message: '请填写离开日期'},
      },
      {
        name: 'endTimeStr',
        rules: {required: true, message: '请填写离开时间'},
      },
      {
        name: 'visitReason',
        rules: {required: true, message: '请填写到访事由'},
      },
      {
        name: 'visitCount',
        rules: {required: true, message: '请填写同行人数'},
      },
      
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.init()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  init(){
    let userStr =  wx.getStorageSync("user");
    let user1:UserDto = JSON.parse(userStr) as UserDto
    this.setData({
      "user":user1
    })
  },
  formInputChange(e:any){
    let field = e.currentTarget.dataset.field
    let value = e.detail.value
    this.setProperty("form",field,value);
  },

  setProperty(obj: string, key: string, value: any): void {
    const path = obj+"."+ key;
    this.setData({
      [path]:value
    })
  },

  selfBtnClick(){
    console.log("快速设置本人信息")
    this.setData({
      "form.applyName":this.data.user.name,
      "form.applyPhone":this.data.user.phone,
      "form.carNo":this.data.user.carNo,
      "form.applyCompany":this.data.user.company
    })
  },

  bindDateChange(e:any){
    console.log(e.currentTarget.dataset)
    console.log(e.detail.value)
    let field = e.currentTarget.dataset.field
    let value = e.detail.value
    this.setProperty("form",field,value);
  },

  bindTimeChange(e:any){
    console.log(e.currentTarget.dataset)
    console.log(e.detail.value)
    let field = e.currentTarget.dataset.field
    let value = e.detail.value
    this.setProperty("form",field,value);
  },
  async submitForm(){
    console.log("formvalid",this.data.form)
    this.selectComponent('#form').validate((valid:any, errors:any) => {
        console.log('valid结果', valid, errors)
        if (!valid) {
            const firstError = Object.keys(errors)
            if (firstError.length) {
                this.setData({
                    error: errors[firstError[0]].message
                })
            }
        } else {
            this.save()
        }
    })
  
  },

  async save(){
      console.log(this.data.form)
        this.setData({
          loading:true
        })
        let res = await save(this.data.form)
        
        if(res.code == LENGYU_SUCCESS_CODE){
          wx.navigateBack({
            delta: 1
          })
        }
        else{
          this.setData({
              error: res.msg
          })
        }
      
        this.setData({
          loading:false
        })
  }
})